package com.mobzilla.activity;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.mobzilla.model.Bug;
import com.mobzilla.service.ServiceFactory;
import com.mobzilla.util.Constants;
import com.mobzilla.util.UIUtil;

public class CustomAdapter extends BaseAdapter
{

	private Activity activity;
	private static LayoutInflater inflater = null;
	private ListItemListener[] mListItemListeners;

	public CustomAdapter(Activity a)
	{
		activity = a;
		inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		mListItemListeners = new ListItemListener[]{
				new ListItemListener()
		      		{
					@Override
					public void onClick(View v) 
					{
						Intent i = new Intent(activity, ReportBugActivity.class);
						activity.startActivity(i);
					}
		      		},
      				new ListItemListener()
      				{
      					@Override
      					public void onClick(View v) {
      						List<Bug> bugList = ServiceFactory.getSearchServiceInstance().fetchAllShowStoppers();
      						if (!bugList.isEmpty())
      						{
      							BugListActivity.setBugList(bugList);
      							Intent i = new Intent(activity, BugListActivity.class);
      							activity.startActivity(i);
      						}
      						else
      						{
      							Toast.makeText(activity.getApplicationContext(), "No Bugs Foud.",  Toast.LENGTH_SHORT).show();
      						}
      					}
      				},
      				new ListItemListener()
      				{
      					@Override
      					public void onClick(View v) {
      						List<Bug> bugList = ServiceFactory.getSearchServiceInstance().fetchDefectsForUser(Constants.getCurrentUser().getUsername());
      						if (!bugList.isEmpty())
      						{
      							BugListActivity.setBugList(bugList);
      							Intent i = new Intent(activity, BugListActivity.class);
      							activity.startActivity(i);
      						}
      						else
      						{
      							Toast.makeText(activity.getApplicationContext(), "No Bugs Foud.",  Toast.LENGTH_SHORT).show();
      						}									
      					}
      				},
      				new ListItemListener()
      				{
      					@Override
      					public void onClick(View v) {
      						Intent i = new Intent(activity, SaveActivity.class);
      						activity.startActivity(i);										
      					}
      				},
      				new ListItemListener()
      				{
      					@Override
      					public void onClick(View v) {
      						List<Bug> bugList = ServiceFactory.getSearchServiceInstance().fetchOpenDefects();
      						if (!bugList.isEmpty())
      						{
      							BugListActivity.setBugList(bugList);
      							Intent i = new Intent(activity, BugListActivity.class);
      							activity.startActivity(i);
      						}
      						else
      						{
      							Toast.makeText(activity.getApplicationContext(), "No Bugs Foud.",  Toast.LENGTH_SHORT).show();
      						}								
      					}
      				},
      				new ListItemListener()
      				{
      					@Override
      					public void onClick(View v) {
      						List<Bug> bugList = ServiceFactory.getSearchServiceInstance().fetchResolvedDefects();
      						if (!bugList.isEmpty())
      						{
      							BugListActivity.setBugList(bugList);
      							Intent i = new Intent(activity, BugListActivity.class);
      							activity.startActivity(i);
      						}
      						else
      						{
      							Toast.makeText(activity.getApplicationContext(), "No Bugs Foud.",  Toast.LENGTH_SHORT).show();
      						}
      					}
      				},
      				
      				new ListItemListener()
      				{
      					@Override
      					public void onClick(View v) 
      					{      						      						
      						Intent i = new Intent(activity, LoadActivity.class);
  							activity.startActivity(i);
      					}
      				},
      				new ListItemListener()
      				{
      					@Override
      					public void onClick(View v) 
      					{      						      						
      						Intent i = new Intent(activity, ExperienceActivity.class);
  							activity.startActivity(i);
      					}
      				},
      				new ListItemListener()
      				{
      					@Override
      					public void onClick(View v) {
      						UIUtil.killApp(true);		
      					}
      				}				
      		 };                                		
	}

	public int getCount()
	{
		return images.length;
	}

	public Object getItem(int position)
	{
		return position;
	}

	public long getItemId(int position)
	{
		return position;
	}

	public static class ViewHolder
	{
		public TextView title;
		public TextView detail;
		public ImageView image;
	}

	public View getView(int position, View convertView, ViewGroup parent)
	{
		View vi = convertView;
		ViewHolder holder;
		if (convertView == null)
		{
			vi = inflater.inflate(R.layout.menulist, null);
			holder = new ViewHolder();
			holder.title = (TextView) vi.findViewById(R.id.title);
			holder.detail = (TextView) vi.findViewById(R.id.detail);
			holder.image = (ImageView) vi.findViewById(R.id.image);
			vi.setClickable(true);
			vi.setOnClickListener(mListItemListeners[position]);
			vi.setTag(holder);
		}
		else
			holder = (ViewHolder) vi.getTag();

		holder.title.setText(texts[position]);
		holder.detail.setText(subTexts[position]);
		holder.image.setTag(texts[position]);
		holder.image.setImageResource(images[position]);
		return vi;
	}

	private static String[] texts =
	{ 
		"Report a Bug",
		"Hot Bugs", 
		"My Bugs", 
		"Search Bugs", 
		"Unresolved Bugs", 
		"Resolved Bugs",
		"Load Balancing",
		"Experience Factor",
		"Exit" 
	};

	private static String[] subTexts =
	{ 
		"Report a new defect.",
		"List of all the P1 and S1 bugs.", 
		"List of all bugs assigned to me.", 
		"Search Bugs with a given ID.", 
		"List of all unresolved bugs.", 
		"List of all resolved bugs.", 
		"View current workload of users.",
		"Max experience per component by any user.",
		"Exit Mobzilla." 
	};

	private static Integer[] images =
	{ 
		R.drawable.add1bug,
		R.drawable.hotbugs, 
		R.drawable.mybugs, 
		R.drawable.search_bug, 
		R.drawable.openbugs, 
		R.drawable.resolved, 
		R.drawable.load,
		R.drawable.experience,
		R.drawable.exit, 
	};
}

class ListItemListener implements OnClickListener
{
	@Override
	public void onClick(View v) {
				
	}
}